<template>
  <div class="container">
    <div class="app-container">
      <!-- 添加权限按钮 -->
      <div style="padding: 10px">
        <el-button
          type="primary"
          size="mini"
          @click="addDialog('add',0,1)"
        >添加权限</el-button>
      </div>
      <!-- 权限数据表格 -->
      <el-table :data="permList" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button v-if="row.type === 1" type="text" size="mini" @click="addDialog('add',row.id,2)">添加</el-button>
            <el-button type="text" size="mini" @click="addDialog('edit',row.id)">编辑</el-button>
            <el-button type="text" size="mini" @click="delPermisson(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹层组件 -->
    <add-permissions
      :id="id"
      ref="openDialog"
      :dialog-visible.sync="dialogVisible"
      :type="type"
      :ser="ser"
      @sxym="getPermissionList"
    />
  </div>
</template>
<script>
import { getPermissionsAPI, delPermissionsAPI } from '@/api/permisson'
import { tranListToTreeDate } from '@/utils'
import addPermissions from './add-permissions.vue'
export default {
  name: 'Permission',
  components: {
    // testFormVue
    addPermissions
  },
  data() {
    return {
      permList: [],
      dialogVisible: false,
      type: 0,
      id: 0,
      ser: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.permList = tranListToTreeDate(await getPermissionsAPI())
    },
    addDialog(ser, id, type) {
      this.dialogVisible = true
      if (ser === 'add') {
        this.ser = '新增'
        this.type = type
        this.id = id
      } else {
        this.id = id
        // console.log(ser, id, type)
        this.ser = '编辑'
        this.$nextTick(() => {
          this.$refs.openDialog.getPermissionsXq()
        })
      }

      // this.$nextTick(() => {
      //   this.$refs.openDialog.addPermission()
      // })
    },
    async delPermisson(id) {
      await delPermissionsAPI(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    }
  }
}
</script>
